package com.guchenbo.spark.core

import org.apache.spark.{SparkConf, SparkContext}

import scala.math.random

/** Computes an approximation to pi */
object SparkPi {
  def main(args: Array[String]) {
    println("in spark pi")
    var conf = new SparkConf().setAppName("Spark Pi").setMaster("local")
    var sc = new SparkContext(conf)
    val slices = if (args.length > 0) args(0).toInt else 2
    val n = math.min(100000L * slices, Int.MaxValue).toInt // avoid overflow
    val count = sc.parallelize(1 until n, slices).map { i =>
      val x = random * 2 - 1
      val y = random * 2 - 1
      if (x * x + y * y <= 1) 1 else 0
    }.reduce(_ + _)
    println(s"Spark Pi is roughly ${4.0 * count / (n - 1)}!")
  }
}
